﻿package com._public._control 
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 按钮容器，用于组件开发中，处理四种钮按状态
	 * @author WZH(shch8.com)
	 */
	public class ButtonCase extends Sprite
	{
		private var _disabledSkin:Sprite
		private var btn:SimpleButton;
		
		public var upName:String = "";
		public var downName:String = "";
		public var overName:String = "";
		public var disabledName:String = "";
		/**
		 * 创建时回调事件
		 * 例：createClip(className:String):Sprite
		 */
		public var callCreate:Function;
		/**
		 * 点击时回调
		 * 例：clickEvent(btn:ButtonCase):void
		 */
		public var callClick:Function;
		
		//状态常量
		public const UP:String = "up";
		public const DOWN:String = "down";
		public const OVER:String = "over";
		public const DISABLED:String = "disabled";
		
		private var sprAdjust:SprAdjust
		private var _enabled:Boolean
		public function ButtonCase(_upName:String="",_downName:String="",_overName:String="",_disabledName:String="") 
		{
			btn = new SimpleButton
			addChild(btn)
			
			upName = _upName
			downName = _downName
			overName = _overName
			disabledName = _disabledName
			addEventListener(Event.ADDED_TO_STAGE,init)
		}
		private function init(e:Event):void {
			btn.upState = createButton(upName)
			btn.downState = createButton(downName)
			btn.overState = createButton(overName)
			btn.hitTestState=btn.upState
			if(disabledName!="")disabledSkin=createButton(disabledName)
			removeEventListener(Event.ADDED_TO_STAGE, init)
		}
		public function set enabled(value:Boolean):void {
			btn.visible = value;
			if (disabledSkin != null) disabledSkin.visible = !value;
			_enabled = value;
		}
		public function get enabled():Boolean{
			return _enabled;
		}
		
		private function createButton(cName:String=""):Sprite {
			var createSpr:Sprite
			if ((callCreate is Function) && cName != "") {
				createSpr = callCreate(cName);
			}else {
				trace("ButtonCase中没有"+(cName==""?"创建名称":"回调方法"))
				}
			if (createSpr == null) return null;
			createSpr.tabEnabled=createSpr.mouseEnabled = false
			return createSpr
		}
		/**
		 * 失效
		 */
		public function set disabledSkin(value:Sprite):void { _disabledSkin = value; _disabledSkin.visible = !enabled; addChild(_disabledSkin) }
		public function get disabledSkin():Sprite { return _disabledSkin };
	}
	
}